package org.jiuling.aliyun.sms.factory;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import darabonba.core.client.ClientOverrideConfiguration;
import org.jiuling.aliyun.sms.config.AliYunSmsConfig;

/**
 * 阿里云短信异步
 * @author 紫灵
 * @date 2023年03月02日 09时48分21秒
 */
public  abstract class AliYunSmsAsynchronous {

    /** 阿里云短信配置 */
    private final AliYunSmsConfig aliYunSmsConfig;

    public AliYunSmsAsynchronous(AliYunSmsConfig aliYunSmsConfig) {
        this.aliYunSmsConfig = aliYunSmsConfig;
    }

    /**
     * 创建异步客户端
     * @return 异步客户端
     */
    public AsyncClient create(){
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(aliYunSmsConfig.getAliYunConfig().getAccessKeyId())
                .accessKeySecret(aliYunSmsConfig.getAliYunConfig().getAccessKeySecret())
                //.securityToken("<your-token>") // use STS token
                .build());
        return AsyncClient.builder()
                // Region ID
                .region(aliYunSmsConfig.getServiceAccessPoint().getAreaId())
                // 使用已配置的HttpClient，否则使用默认的HttpClient (Apache HttpClient)
//                .httpClient(httpClient)
                .credentialsProvider(provider)
                // 服务级别配置
//                .serviceConfiguration(Configuration.create())
                // 客户端级别的配置重写，可以设置端点，Http请求参数等。
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride(aliYunSmsConfig.getServiceAccessPoint().getPublicAccessAddress())
                        //.setConnectTimeout(Duration.ofSeconds(30))
                )
                .build();
    }
}
